package com.gdms.service.impl;

import java.util.*;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gdms.dao.*;
import com.gdms.entity.*;
import com.gdms.service.IStudentService;

@Service
public class StudentServiceImpl implements IStudentService{
	@Autowired
	private IStudentDao studentDao;
	
	@Autowired
	private ISubjectDao subjectDao;
	
	@Autowired
	private ISubjectSelectDao subjectSelectDao;

	@Autowired
	private ITeacherReportDao teacherReportDao;
	
	@Autowired
	private IStudentReportDao studentReportDao;
	
	@Autowired
	private ITeacherProgressDao teacherProgressDao;
	
	@Autowired
	private IAnnouncementDao announcementDao;
	
	@Autowired
	private ISubjectInfoDao subjectInfoDao;
	
	@Autowired
	private IDoubtDao doubtDao;
	
	public int addStudent(Student student) {
		return studentDao.addStudent(student);
	}

	public List<Student> showAllStudent() {
		// TODO Auto-generated method stub
		return studentDao.showAllStudent();
	}

	public Student getStudentNameById(int id) {
		// TODO Auto-generated method stub
		return studentDao.selectStudentById(id);
	}

	public int updateStudent(Student student) {
		// TODO Auto-generated method stub
		return studentDao.updateStudent(student);
	}

	public int deleteStudent(int id) {
		// TODO Auto-generated method stub
		return studentDao.deleteStudent(id);
	}

	public List<Student> showStudentOne1(String studentNo) {
		// TODO Auto-generated method stub
		return studentDao.showStudentOne1(studentNo);
	}

	public List<Student> showStudentOne2(String studentName) {
		// TODO Auto-generated method stub
		return studentDao.showStudentOne2(studentName);
	}

	public List<Student> showStudentOne3(String studentNo, String studentName) {
		// TODO Auto-generated method stub
		return studentDao.showStudentOne3(studentNo, studentName);
	}

	public Student getStudentByNo(String studentNo) {
		// TODO Auto-generated method stub
		return studentDao.getInfoByNo(studentNo);
	}

	public List<Subject> availableSubjectSelect() {
		// TODO Auto-generated method stub

		List<Subject> subjectList = subjectDao.showAllSubject();
		
		for(int i = 0; i < subjectList.size(); i++) {
			if(subjectList.get(i).getStatus() != 2) {
				subjectList.remove(i);
			}
		}

		List<SubjectSelect> subjectSelectList = subjectSelectDao.showAllSubjectSelect();
		List<Subject> showList = new ArrayList<>();
		for(int i = 0; i < subjectList.size(); i++) {
			for(int j = 0; j < subjectSelectList.size(); j++) {
				if(subjectList.get(i).getId() == subjectSelectList.get(j).getSubjectId()) {
					break;
				}else if(j == subjectSelectList.size()-1) {
					showList.add(subjectList.get(i));
				}
			}
		}
		System.out.println(showList);
		return showList;
	}

	public SubjectSelect chosenSubject(int studentId) {
		// TODO Auto-generated method stub
		return subjectSelectDao.subjectSelectByStudentId(studentId);
	}

	public int addSubjectSelect(SubjectSelect subjectSelect) {
		// TODO Auto-generated method stub
		return subjectSelectDao.addSubjectSelect(subjectSelect);
	}

	public int deleteSubjectSelect(int studentId) {
		// TODO Auto-generated method stub
		return subjectSelectDao.deleteSubjectSelect(studentId);
	}

	public TeacherReport getFilePathBySubjectId(int subjectId) {
		// TODO Auto-generated method stub
		return teacherReportDao.showInfo(subjectId);
	}

	public int uploadTaskBook(int studentId, String filePath) {
		// TODO Auto-generated method stub
		StudentReport studentReport = studentReportDao.showInfoByStudentId(studentId);
		if(studentReport == null) {
			studentReportDao.addInfoByStudentId(studentId);
		}
		return studentReportDao.uploadTaskBook(studentId, filePath);
	}

	public int uploadOpeningReport(int studentId, String filePath) {
		// TODO Auto-generated method stub
		StudentReport studentReport = studentReportDao.showInfoByStudentId(studentId);
		if(studentReport == null) {
			studentReportDao.addInfoByStudentId(studentId);
		}
		return studentReportDao.uploadOpeningReport(studentId, filePath);
	}
	
	public int uploadFeasibilityAnalysisReport(int studentId, String filePath) {
		// TODO Auto-generated method stub
		StudentReport studentReport = studentReportDao.showInfoByStudentId(studentId);
		if(studentReport == null) {
			studentReportDao.addInfoByStudentId(studentId);
		}
		return studentReportDao.uploadFeasibilityAnalysisReport(studentId, filePath);
	}
	
	public int uploadDemandAnalysisReport(int studentId, String filePath) {
		// TODO Auto-generated method stub
		StudentReport studentReport = studentReportDao.showInfoByStudentId(studentId);
		if(studentReport == null) {
			studentReportDao.addInfoByStudentId(studentId);
		}
		return studentReportDao.uploadDemandAnalysisReport(studentId, filePath);
	}
	
	public int uploadSummaryDesignReport(int studentId, String filePath) {
		// TODO Auto-generated method stub
		StudentReport studentReport = studentReportDao.showInfoByStudentId(studentId);
		if(studentReport == null) {
			studentReportDao.addInfoByStudentId(studentId);
		}
		return studentReportDao.uploadSummaryDesignReport(studentId, filePath);
	}
	
	public int uploadDatabaseFile(int studentId, String filePath) {
		// TODO Auto-generated method stub
		StudentReport studentReport = studentReportDao.showInfoByStudentId(studentId);
		if(studentReport == null) {
			studentReportDao.addInfoByStudentId(studentId);
		}
		return studentReportDao.uploadDatabaseFile(studentId, filePath);
	}

	public StudentReport getStudentReportInfoByStudentId(int studentId) {
		// TODO Auto-generated method stub
		return studentReportDao.showInfoByStudentId(studentId);
	}

	public Map<String, String> getStudentReportToMap(int studentId) {
		// TODO Auto-generated method stub
		Map<String, String> fileList = new HashMap<String, String>();
		StudentReport studentReport = studentReportDao.showInfoByStudentId(studentId);
		if (studentReport != null) {
			String taskBookPath = studentReport.getTaskBook();
			if (taskBookPath != null) {
				String[] str1 = taskBookPath.split("\\\\");
				String fileName1 = str1[str1.length - 1].toString();
				fileList.put(fileName1, taskBookPath);
			}

			String openingPath = studentReport.getOpeningReport();
			if (openingPath != null) {
				String[] str2 = openingPath.split("\\\\");
				String fileName2 = str2[str2.length - 1].toString();
				fileList.put(fileName2, openingPath);
			}

			String feasibilityAnalysisReportPath = studentReport.getFeasibilityAnalysisReport();
			if (feasibilityAnalysisReportPath != null) {
				String[] str2 = feasibilityAnalysisReportPath.split("\\\\");
				String fileName2 = str2[str2.length - 1].toString();
				fileList.put(fileName2, feasibilityAnalysisReportPath);
			}

			String demandAnalysisReportPath = studentReport.getDemandAnalysisReport();
			if (demandAnalysisReportPath != null) {
				String[] str2 = demandAnalysisReportPath.split("\\\\");
				String fileName2 = str2[str2.length - 1].toString();
				fileList.put(fileName2, demandAnalysisReportPath);
			}

			String summaryDesignReportPath = studentReport.getSummaryDesignReport();
			if (summaryDesignReportPath != null) {
				String[] str2 = summaryDesignReportPath.split("\\\\");
				String fileName2 = str2[str2.length - 1].toString();
				fileList.put(fileName2, summaryDesignReportPath);
			}

			String databaseFilePath = studentReport.getDatabaseFile();
			if (databaseFilePath != null) {
				String[] str2 = databaseFilePath.split("\\\\");
				String fileName2 = str2[str2.length - 1].toString();
				fileList.put(fileName2, databaseFilePath);
			}
		}
		return fileList;
	}

	public int resetTaskBook(int studentId) {
		// TODO Auto-generated method stub
		return studentReportDao.resetTaskBook(studentId);
	}

	public int resetOpeningReport(int studentId) {
		// TODO Auto-generated method stub
		return studentReportDao.resetOpeningReport(studentId);
	}

	public StudentReport getInfoByTaskBookPath(String taskBookPath) {
		// TODO Auto-generated method stub
		return studentReportDao.getInfoByTaskBookPath(taskBookPath);
	}

	public StudentReport getInfoByOpeningReportPath(String openingPath) {
		// TODO Auto-generated method stub
		return studentReportDao.getInfoByOpeningReportPath(openingPath);
	}

	public Subject getThesisInfoById(int thesisId) {
		// TODO Auto-generated method stub
		return subjectDao.getSubjectById(thesisId);
	}

	public List<TeacherProgress> getTeacherProgressByStudentId(int studentId) {
		// TODO Auto-generated method stub
		return teacherProgressDao.getInfoByStudentId(studentId);
	}

	public List<Announcement> showAllAnnouncement() {
		// TODO Auto-generated method stub
		return announcementDao.showAllAnnouncement();
	}

	public int uploadSubjectInfo(int studentId, String filePath) {
		// TODO Auto-generated method stub
		SubjectInfo subjectInfo = new SubjectInfo();
		subjectInfo.setStudentId(studentId);
		subjectInfo.setSubjectFile(filePath);
		return subjectInfoDao.addSubjectInfo(subjectInfo);
	}

	public SubjectInfo getInfoByStudentId(int studentId) {
		// TODO Auto-generated method stub
		return subjectInfoDao.getInfoByStudentId(studentId);
	}

	public SubjectInfo getInfoByFilePath(String filePath) {
		// TODO Auto-generated method stub
		return subjectInfoDao.getInfoByFilePath(filePath);
	}

	public int deleteSubjectInfo(int studentId) {
		// TODO Auto-generated method stub
		return subjectInfoDao.deleteSubjectInfo(studentId);
	}

	public SubjectInfo getSubjectInfoByStudentId(int studentId) {
		// TODO Auto-generated method stub
		return subjectInfoDao.getInfoByStudentId(studentId);
	}

	public int addDoubt(Doubt doubt) {
		// TODO Auto-generated method stub
		return doubtDao.addDoubt(doubt);
	}

	public List<Doubt> getAllDoubtByStudent(int studentId) {
		// TODO Auto-generated method stub
		return doubtDao.getAllDoubt(studentId);
	}


	public String getSubjectDescription(int subjectId) {
		// TODO Auto-generated method stub
		Subject thesis = subjectDao.getSubjectById(subjectId);
		return thesis.getDescription();
	}
}